﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Sinacor.Infra.Service.SecurityTokenService.Services.ServiceImplementations;
using System.ServiceModel.Dispatcher;
using Sinacor.Infra.Service.Security.Authentication.ServiceCredentials;
using System.ServiceModel.Description;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel.Channels;
using Sinacor.Infra.Service.SecurityTokenService.Services.ServiceContracts;
using Sinacor.Infra.Common.Security.Authentication.Credentials;
using System.ServiceModel.Security.Tokens;

namespace Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(SecurityTokenService)))
            {

                ServiceCredentials sc = host.Credentials;
                X509Certificate2 cert = sc.ServiceCertificate.Certificate;
                SinacorServiceCredentials serviceCredential = new SinacorServiceCredentials();

                serviceCredential.ServiceCertificate.Certificate = cert;

                serviceCredential.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine,StoreName.TrustedPeople,X509FindType.FindBySubjectDistinguishedName,"CN=SinacorSTSAuthority");

                host.Description.Behaviors.Remove((typeof(ServiceCredentials)));
                host.Description.Behaviors.Add(serviceCredential);


                Binding sinacorBinding = CreateSinacorBinding();
                host.AddServiceEndpoint(typeof(ISecurityTokenService), sinacorBinding, "http://localhost:62002/SecurityTokenService");

                host.Open();



                foreach (ChannelDispatcher cd in host.ChannelDispatchers)
                    foreach (EndpointDispatcher ed in cd.Endpoints)
                        Console.WriteLine("Service listening at {0}", ed.EndpointAddress.Uri);

                // The service can now be accessed.
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();

                Console.ReadLine();
            }
       }

        public static Binding CreateSinacorBinding()
        {
            HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();

            // the message security binding element will be configured to require a credit card
            // token that is encrypted with the service's certificate 
            SymmetricSecurityBindingElement messageSecurity = new SymmetricSecurityBindingElement();
            messageSecurity.EndpointSupportingTokenParameters.SignedEncrypted.Add(new SinacorTokenParameters());
            X509SecurityTokenParameters x509ProtectionParameters = new X509SecurityTokenParameters();
            x509ProtectionParameters.InclusionMode = SecurityTokenInclusionMode.Never;
            messageSecurity.ProtectionTokenParameters = x509ProtectionParameters;
            return new CustomBinding(messageSecurity, httpTransport);
        }
    }
}
